﻿using System;
using System.Collections.Generic;
using gms.data;
using gms.security;
using gms.system;

namespace gms.page.admin {
	public partial class playermgr_edit : GMSPage {
		public bool posted = false;
		public Player player;
		public IList<Acl_Group> groups;

		public playermgr_edit() : base() {
			this.Load += new EventHandler(playermgr_edit_Load);
		}

		void playermgr_edit_Load(object sender, EventArgs e) {
			Authorization.AuthorizeOrForward(AclType.ManagePlayers);

			int player_id = 0;
			bool changed_email = false;

			if (Request.QueryString.Count != 0) {
				if (Request.QueryString["id"].Length != 0) {
					if (!int.TryParse(Request.QueryString["id"], out player_id)) {
						Response.Redirect("playermgr.aspx");
					}
				}
				else {
					Response.Redirect("playermgr.aspx");
				}
			}
			else {
				Response.Redirect("playermgr.aspx");
			}

			if (Request.Form.Count != 0) {
				posted = true;
			}

			player = nhdb.session.Get<Player>(player_id);
			groups = nhdb.session.CreateCriteria<Acl_Group>().List<Acl_Group>();

			if (posted) {
				if (Request.Form["player_first_name"] != null) {
					if (Request.Form["player_first_name"].Length != 0) {
						if (player.first_name != Request.Form["player_first_name"]) {
							player.first_name = Request.Form["player_first_name"];
						}
					}
					else {
						posted = false;
						throw new Exception("First Name must not be empty");
					}
				}
				else {
					posted = false;
					throw new Exception("First Name must not be empty");
				}

				if (Request.Form["player_last_name"] != null) {
					if (Request.Form["player_last_name"].Length != 0) {
						if (player.last_name != Request.Form["player_last_name"]) {
							player.last_name = Request.Form["player_last_name"];
						}
					}
				}

				if (Request.Form["player_email"] != null) {
					if (Request.Form["player_email"].Length != 0) {
						if (player.email != Request.Form["player_email"]) {
							changed_email = true;
							player.email = Request.Form["player_email"];
						}
					}
					else {
						posted = false;
						throw new Exception("Email must not be empty");
					}
				}
				else {
					posted = false;
					throw new Exception("Email must not be empty");
				}
			}

			if (posted && changed_email) {
				if (Player.email_exists(player.email)) {
					posted = false;
					throw new Exception("Email is already in use");
				}
			}

			if (posted) {
				nhdb.session.BeginTransaction();
				nhdb.session.Update(player);
				nhdb.session.Transaction.Commit();
			}

			if (posted && Authorization.IsAuthorized(AclType.ManageAclgroups)) {
				Player_Aclgroup.delete_by_player(player.player_id);

				int temp_val = -1;

				foreach (string key in Request.Form.Keys) {
					if (key.IndexOf("group_") != -1) {
						if (int.TryParse(Request.Form[key], out temp_val)) {
							Player_Aclgroup.create(player.player_id, temp_val);
						}
					}
				}
			}

			if (posted) {
				Response.Redirect("playermgr.aspx");
			}
		}
	}
}